package com.example.controller;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.model.UserAddressDTO;
import com.example.model.req.UpdateFavoriteReq;
import com.example.model.req.UserLoginReq;
import com.example.model.resp.ListEmloyeeInfoResp;
import com.example.model.resp.ListEmployeeInMiniResp;
import com.example.model.resp.ListFavoriteEmployeeResp;
import com.example.model.resp.UserInfoResp;
import com.example.service.UserService;
import com.example.utils.ImageUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@ResponseBody
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ImageUtil imageUtil;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/login")
    public R<String> userLogin(@RequestBody UserLoginReq userLoginReq) {
        return userService.userLogin(userLoginReq);
    }

    @GetMapping("/detail")
    public R<UserInfoResp> userDetail(HttpServletRequest request) {
        return userService.userDetail(request);
    }

    @GetMapping("/listEmployee/{serviceId}")
    public R<List<ListEmployeeInMiniResp>> listEmployee(@PathVariable("serviceId") Integer serviceId) {
        return userService.listEmployee(serviceId, request);
    }

    // 收藏护工
    @PostMapping("/updateFavorite")
    public R<String> updateFavorite(UpdateFavoriteReq updateFavoriteReq) {
        return userService.updateFavorite(updateFavoriteReq, request);
    }

    // 获取用户的所有收藏的护工
    @GetMapping("/listFavorite")
    public R<List<ListFavoriteEmployeeResp>> getFavoriteList(@RequestParam("userId") Integer userId) {
        if (userId == null) {
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        return userService.getFavoriteList(userId);
    }

    @PostMapping("/upload")
    public String uploadAvatar(@RequestParam("avatar") MultipartFile filePath)
            throws IOException {
        String fileName = filePath.getOriginalFilename();
        String imgName = imageUtil.getRandomImgName(fileName);
        if (!filePath.isEmpty()) {
            FileInputStream inputStream = (FileInputStream) filePath.getInputStream();
            String path = imageUtil.uploadImageQiniu(inputStream, imgName);
            return path;
        }
        return "";
    }

    @PostMapping("/updateAddress")
    public R<String> updateAddress(@RequestParam(name = "oldAddressId", required = false) String oldAddressId,
            @RequestParam("newAddressId") String newAddressId) {
        if (oldAddressId.equals("undefined")) {
            return userService.updateAddress(request, null, Integer.valueOf(newAddressId));
        }
        return userService.updateAddress(request, Integer.valueOf(oldAddressId), Integer.valueOf(newAddressId));
    }

    @GetMapping("/addressListByUserId")
    public R<List<UserAddressDTO>> getAddressListByUserId() {
        return userService.getAddressListByUserId(request);
    }

    @PostMapping("/addNewAddress")
    public R<String> addNewAddress(@RequestParam("newAddress") String newAddress) {
        return userService.addNewAddress(newAddress, request);
    }

    @PostMapping("/updateAvatar")
    public R<String> upadateAvatar(@RequestBody String avatar) {
        return userService.updateAvatar(avatar, request);
    }

    @PostMapping("/updateGender")
    public R<Boolean> updateGender(@RequestBody boolean gender) {
        return userService.updateGender(gender, request);
    }

    @GetMapping("/employeeDetail/{employeeId}")
    public R<ListEmloyeeInfoResp> employeeDetail(@PathVariable("employeeId") Integer employeeId) {
        return userService.employeeDetail(employeeId);
    }

    @PostMapping("/chargeBalance/{chargeVal}")
    public R<String> chargeBalance(@PathVariable("chargeVal") BigDecimal chargeVal) {
        return userService.chargeBalance(chargeVal, request);
    }

    @GetMapping("/queryBalance")
    public R<BigDecimal> queryBalance() {
        return userService.queryBalance(request);
    }
}